#include <ansi_c.h>
#define SIZE 9
#define nr 'n'
#define bl 'b'
#define ab ' '
#define x 'x'
#define tmp 'B'
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
														/*PROTOTYPE DES FONCTIONS*/
void nr_return_pion(char,int,int);
char nr_return_ht_gch(char,int,int);
char nr_return_ht_ct(char,int,int);
char nr_return_ht_drt(char,int,int);
char nr_return_ml_gch(char,int,int);
char nr_return_ml_drt(char,int,int);
char nr_return_bs_gch(char,int,int);
char nr_return_bs_ct(char,int,int);
char nr_return_bs_drt(char,int,int);
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
														/*PROTOTYPE DES FONCTIONS*/  
void nr_detection(char tab[SIZE][SIZE]);
char nr_case_haut_gauche(char,int,int);
char nr_case_haut_centre(char,int,int);   
char nr_case_haut_droite(char,int,int);  
char nr_case_milieu_gauche(char,int,int);  
char nr_case_milieu_droite(char,int,int);  
char nr_case_bas_gauche(char,int,int);   
char nr_case_bas_centre(char,int,int);  
char nr_case_bas_droite(char,int,int);

/*--------------------------------------------------------------------------------------------------------------------------------------------*/
													   /*VARIABLES GENERALE*/
char tab[SIZE][SIZE]= { {ab,'1','2','3','4','5','6','7','8'},
						{'1',ab,ab,ab,ab,ab,ab,ab,ab},
 				 		{'2',ab,ab,ab,ab,ab,ab,ab,ab},		
 						{'3',ab,ab,ab,ab,ab,ab,ab,ab},
 				 		{'4',ab,ab,bl,bl,nr,ab,ab,ab},
						{'5',ab,ab,bl,nr,bl,ab,ab,ab},
						{'6',ab,ab,ab,ab,ab,ab,ab,ab},
 					    {'7',ab,ab,ab,ab,ab,ab,ab,ab},
						{'8',ab,ab,ab,ab,ab,ab,ab,ab},
					  };
int colonne;
int ligne;
char case_next;
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
														/*FONCTION MAIN*/
void main(void)
{
 int i;
 int j;
 int pion_b=0;
 int pion_n=0;
 int fin=0;
 do
 {
 nr_detection(tab);
 for(i=0;i<SIZE;i++)				 
 {									 
  printf("\n");
  for(j=0;j<SIZE;j++)				  
  {
   printf("\t%c",tab[i][j]);
   if(tab[i][j]==nr)
	   pion_b=pion_b+1;
   else if (tab[i][j]==bl)
	   pion_n=pion_n+1;
  }
 printf("\n");
 }
 printf("\n");
 printf("\npoint blanc :%d\t\tpoint noir :%d",pion_b,pion_n);
 }
 while(pion_b != 0 );
}
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
														/*FONCTION PRINCIPALE DE DETECTION DES PIONS*/
void nr_detection(char tab[SIZE][SIZE])
{
 int i;
 int j;
 int pion_b=0;
 int pion_n=0;
 int k=0;
 for(i=0 ; i<SIZE ; i++)
 {
  for(j=0 ; j<SIZE ; j++)
  {
   if(tab[i][j]==nr)
   {
	tab[i-1][j-1]=nr_case_haut_gauche(tab[i-1][j-1],i,j);
	tab[i-1][j]=nr_case_haut_centre(tab[i-1][j],i,j);   
	tab[i-1][j+1]=nr_case_haut_droite(tab[i-1][j+1],i,j);   	   
	tab[i][j-1]=nr_case_milieu_gauche(tab[i][j-1],i,j);   
	tab[i][j+1]=nr_case_milieu_droite(tab[i][j+1],i,j);   
	tab[i+1][j-1]=nr_case_bas_gauche(tab[i+1][j-1],i,j);   
	tab[i+1][j]=nr_case_bas_centre(tab[i+1][j],i,j);   
	tab[i+1][j+1]=nr_case_bas_droite(tab[i+1][j+1],i,j);   
   }
  }
 }
/*------------------------------------------------------------------------------------------------------------------------------------------*/
 														/*AFFICHE LE TABLEAUX*/
 for(i=0;i<SIZE;i++)				 
 {									 
  printf("\n");
  for(j=0;j<SIZE;j++)				  
  {
   printf("\t%c",tab[i][j]);
   if(tab[i][j]==nr)
	   pion_b=pion_b+1;
   else if (tab[i][j]==bl)
	   pion_n=pion_n+1;
  }
 printf("\n");
 }
 printf("\n");
 printf("\npoint blanc :%d\t\tpoint noir :%d",pion_b,pion_n);
 printf("\nligne :");
 scanf("%d",&ligne);
 printf("\ncolonne :");
 scanf("%d",&colonne);
 tab[ligne][colonne]=tmp;
 nr_return_pion(tab[ligne][colonne],ligne,colonne);
}
/*------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_case_haut_gauche(char caract,int i,int j)
{
 if(caract==nr)
 {
 }
 else if(caract==bl)
 {
  if(tab[i-2][j-2]==ab)
  {
   tab[i-2][j-2]=x;
  }
  else
  {
   do
   {
    case_next=tab[i-2][j-2];
    i=i-1;
    j=j-1;
   }
   while(case_next==nr);
   if(case_next==nr)
   {
    tab[i-1][j-1]=case_next;
   }
   else if(case_next==ab)
   {
	case_next=x;
    tab[i-1][j-1]=case_next;
   }
  }
 }
 return caract;
}
/*------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_case_haut_centre(char caract,int i,int j)
{
 if(caract==nr)
 {
 }
 else if(caract==bl)
 {
  if(tab[i-2][j]==ab)
  {
   tab[i-2][j]=x;
  }
  else
  {
   do
   {
    case_next=tab[i-2][j];
    i=i-1;
   }
   while(case_next==bl);
   if(case_next==nr)
   {
    tab[i-1][j]=case_next;
   }
   else if(case_next==ab)
   {
	case_next=x;
    tab[i-1][j]=case_next;
   }
  }
 }
 return caract;
}
/*------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_case_haut_droite(char caract,int i,int j)
{
 if(caract==nr)
 {
 }
 else if(caract==bl)
 {
  if(tab[i-2][j+2]==ab)
  {
   tab[i-2][j+2]=x;
  }
  else
  {
   do
   {
    case_next=tab[i-2][j+2];
    i=i-1;
    j=j+1;
   }
   while(case_next==bl);
   if(case_next==nr)
   {
    tab[i-1][j+1]=case_next;
   }
   else if(case_next==ab)
   {
	case_next=x;
    tab[i-1][j+1]=case_next;
   }
  }
 }
 return caract;
}
/*------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_case_milieu_gauche(char caract,int i,int j)
{
 if(caract==nr)
 {
 }
 else if(caract==bl)
 {
  if(tab[i][j-2]==ab)
  {
   tab[i][j-2]=x;
  }
  else
  {
   do
   {
    case_next=tab[i][j-2];
    j=j-1;
   }
   while(case_next==bl);
   if(case_next==nr)
   {
    tab[i][j-1]=case_next;
   }
   else if(case_next==ab)
   {
	case_next=x;
    tab[i][j-1]=case_next;
   }
  }
 }
 return caract;
}
/*------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_case_milieu_droite(char caract,int i,int j)
{
 if(caract==nr)
 {
 }
 else if(caract==bl)
 {
  if(tab[i][j+2]==ab)
  {
   tab[i][j+2]=x;
  }
  else
  {
   do
   {
    case_next=tab[i][j+2];
    j=j+1;
   }
   while(case_next==bl);
   if(case_next==nr)
   {
    tab[i][j+1]=case_next;
   }
   else if(case_next==ab)
   {
	case_next=x;
    tab[i][j+1]=case_next;
   }
  }
 }
 return caract;
}
/*------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_case_bas_gauche(char caract,int i,int j)
{
 if(caract==nr)
 {
 }
 else if(caract==bl)
 {
  if(tab[i+2][j-2]==ab)
  {
   tab[i+2][j-2]=x;
  }
  else
  {
   do
   {
    case_next=tab[i+2][j-2];
    i=i+1;
    j=j-1;
   }
   while(case_next==bl);
   if(case_next==nr)
   {
    tab[i+1][j-1]=case_next;
   }
   else if(case_next==ab)
   {
	case_next=x;
    tab[i+1][j-1]=case_next;
   }
  }
 }
 return caract;
}
/*------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_case_bas_centre(char caract,int i,int j)
{
 if(caract==nr)
 {
 }
 else if(caract==bl)
 {
  if(tab[i+2][j]==ab)
  {
   tab[i+2][j]=x;
  }
  else
  {
   do
   {
    case_next=tab[i+2][j];
    i=i+1;
   }
   while(case_next==bl);
   if(case_next==nr)
   {
    tab[i+1][j]=case_next;
   }
   else if(case_next==ab)
   {
	case_next=x;
    tab[i+1][j]=case_next;
   }
  }
 }
 return caract;
}
/*------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_case_bas_droite(char caract,int i,int j)
{
 if(caract==nr)
 {
 }
 else if(caract==bl)
 {
  if(tab[i+2][j+2]==ab)
  {
   tab[i+2][j+2]=x;
  }
  else
  {
   do
   {
    case_next=tab[i+2][j+2];
    i=i+1;
    j=j+1;
   }
   while(case_next==bl);
   if(case_next==nr)
   {
    tab[i+1][j+1]=case_next;
   }
   else if(case_next==ab)
   {
	case_next=x;
    tab[i+1][j+1]=case_next;
   }
  }
 }
 return caract;
}
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
														/*FONCTION PRINCIPALE DE RETOURNEMENT DES PIONS*/
void nr_return_pion(char caract,int ligne,int colonne)
{
 int i=ligne;		
 int j=colonne;
 int b=0;
 int n=0;
 tab[i][j]=nr_return_ht_gch(tab[i-1][j-1],i,j);
 tab[i][j]=nr_return_ht_ct(tab[i-1][j],i,j);
 tab[i][j]=nr_return_ht_drt(tab[i-1][j+1],i,j);
 tab[i][j]=nr_return_ml_gch(tab[i][j-1],i,j);
 tab[i][j]=nr_return_ml_drt(tab[i][j+1],i,j);
 tab[i][j]=nr_return_bs_gch(tab[i+1][j-1],i,j);
 tab[i][j]=nr_return_bs_ct(tab[i+1][j],i,j);
 tab[i][j]=nr_return_bs_drt(tab[i+1][j+1],i,j);
 for(i=0;i<SIZE;i++)
 {
  for(j=0;j<SIZE;j++)
  {
   if(tab[i][j]==x)
   {
    tab[i][j]=ab;
   }
  }
 }
}
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
														/*FONCTION SECONDAIRE DE RETOURNEMENT DES PIONS*/
char nr_return_ht_gch(char case_b,int col,int lig)
{
 int i;
 int j;
 char test;
 char case_tmp=nr;
 i=col;
 j=lig;
 if(case_b==bl)
 {
  do
  {
   test=tab[i-1][j-1];
   i=i-1;
   j=j-1;
  }
  while(test==bl);
  if(test==nr)
  {
   do
   {
    case_tmp=tab[col-1][lig-1];
    tab[col-1][lig-1]=nr;
    col=col-1;
    lig=lig-1;
   }
   while(case_tmp==bl);
   case_b=nr;
  }
 }
 return case_tmp;
}
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_return_ht_ct(char case_b,int col,int lig)
{
 int i;
 int j;
 char test ;
 char case_tmp=nr;
 i=col;
 j=lig;
 if(case_b==bl)
 {
  do
  {
   test=tab[i-1][j];
   i=i-1;
  }
  while(test==bl);
  if(test==nr)
  {
   do
   {
    case_tmp=tab[col-1][lig];
    tab[col-1][lig]=nr;
    col=col-1;
   }
   while(case_tmp==bl);
   case_b=nr;
  }
 }
 return case_tmp;
}
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_return_ht_drt(char case_b,int col,int lig)
{
 int i;
 int j;
 char test;
 char case_tmp=nr;
 i=col;
 j=lig;
 if(case_b==bl)
 {
  do
  {
   test=tab[i-1][j+1];
   i=i-1;
   j=j+1;
  }
  while(test==bl);
  if(test==nr)
  {
   do
   {
    case_tmp=tab[col-1][lig+1];
    tab[col-1][lig+1]=nr;
    col=col-1;
    lig=lig+1;
   }
   while(case_tmp==bl);
   case_b=nr;
  }
 }
 return case_tmp;
}
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_return_ml_gch(char case_b,int col,int lig)
{
 int i;
 int j;
 char test;
 char case_tmp=nr;
 i=col;
 j=lig;
 if(case_b==bl)
 {
  do
  {
   test=tab[i][j-1];
   j=j-1;
  }
  while(test==bl);
  if(test==nr)
  {
   do
   {
    case_tmp=tab[col][lig-1];
    tab[col][lig-1]=nr;
    lig=lig-1;
   }
   while(case_tmp==bl);
   case_b=nr;
  }
 }
 return case_tmp;
}
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_return_ml_drt(char case_b,int col,int lig)
{
 int i;
 int j;
 char test;
 char case_tmp=nr;
 i=col;
 j=lig;
 if(case_b==bl)
 {
  do
  {
   test=tab[i][j+1];
   j=j+1;
  }
  while(test==bl);
  if(test==nr)
  {
   do
   {
    case_tmp=tab[col][lig+1];
    tab[col][lig+1]=nr;
    lig=lig+1;
   }
   while(case_tmp==bl);
   case_b=nr;
  }
 }
 return case_tmp;
}
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_return_bs_gch(char case_b,int col,int lig)
{
 int i;
 int j;
 char test;
 char case_tmp=nr;
 i=col;
 j=lig;
 if(case_b==bl)
 {
  do
  {
   test=tab[i+1][j-1];
  i=i+1;
  j=j-1;
  }
  while(test==bl);
  if(test==nr)
  {
   do
   {
    case_tmp=tab[col+1][lig-1];
    tab[col+1][lig-1]=nr;
    col=col+1;
    lig=lig-1;
   }
   while(case_tmp==bl);
   case_b=nr;
  }
 }
 return case_tmp;
}
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_return_bs_ct(char case_b,int col,int lig)
{
 int i;
 int j;
 char test;
 char case_tmp=nr;
 i=col;
 j=lig;
 if(case_b==bl)
 {
  do
  {
   test=tab[i+1][j];
  i=i+1;
  }
  while(test==bl);
  if(test==nr)
  {
   do
   {
    case_tmp=tab[col+1][lig];
    tab[col+1][lig]=nr;
    col=col+1;
   }
   while(case_tmp==bl);
   case_b=nr;
  }
 }
 return case_tmp;
}
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
char nr_return_bs_drt(char case_b,int col,int lig)
{
 int i;
 int j;
 char test;
 char case_tmp=nr;
 i=col;
 j=lig;

 if(case_b==bl)
 {
  do
  {
   test=tab[i+1][j+1];
   i=i+1;
   j=j+1;
  }
  while(test==bl);
  if(test==nr)
  {
   do
   {
    case_tmp=tab[col+1][lig+1];
    tab[col+1][lig+1]=nr;
    col=col-1;
    lig=lig+1;
   }
   while(case_tmp==bl);
   case_b=nr;
  }
 }
 return case_tmp;
}
/*--------------------------------------------------------------------------------------------------------------------------------------------*/
